<template>
  <div>
    <Card style="margin-bottom: 10px;" >
      <!-- <div ref="chartRef" style="height: 280px; width: 100%;"></div> -->
      <LineMulti :chartData="lineMultiData" height="30vh" type="line" :option="lineOptions"></LineMulti>
    </Card>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:clear-outlined" type="primary" @click="handleClean" style="margin-right: 5px"
          danger>清空</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #account="{ record }">
        <Tooltip placement="top" :title="record.userName">
          <span> {{ record.userName }} </span>
        </Tooltip>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <FormDetail @register="registerDetail" />
  </div>
</template>
<script lang="ts" name="monitor-login-log" setup>
import { ref, onMounted } from 'vue';
import { Card, Tooltip } from 'ant-design-vue';
import LineMulti from '/@/components/chart/LineMulti.vue';
import FormDetail from './Detail.vue';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage'
import { getLogList, cleanLog, deleteLog, batchDeleteLog } from './api';
import { queryStatistic } from '/@/api/index';
import { columns, searchFormSchema } from './data';
import { useMessage } from '/@/hooks/web/useMessage';
import { useDrawer } from '/@/components/Drawer';
import { Icon } from '/@/components/Icon';

const [registerDetail, { openDrawer: openDetail }] = useDrawer();

const { createMessage } = useMessage();
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  designScope: 'quartz-template',
  tableProps: {
    title: '日志列表',
    api: getLogList,
    columns: columns,
    canResize: false,
    actionColumn: {
      width: 110,
      title: '操作',
      dataIndex: 'action',
      fixed: false,
      slots: { customRender: 'action' },
    },
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      fieldMapToTime: [['loginTime', ['startTime', 'endTime'], 'YYYY-MM-DD HH:mm:ss']],
    },
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

const lineMultiData = ref<any[]>([]);

const lineOptions = ref({
  title: { text: '周统计', textStyle: { fontWeight: 'lighter' } },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      lineStyle: {
        width: 1,
        color: '#019680',
      },
    },
  },
});

onMounted(async () => {
  lineMultiData.value = await queryStatistic({ days: 7, biz: 'monitor', method: 'login' })
})

/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    },
    {
      label: '删除',
      color: 'error',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      }
    }
  ];
}

const handleClean = () => cleanLog(reload)
const handleDetail = (record) => openDetail(true, { record })
const handleDelete = async (record) => await deleteLog({ id: record.id }, reload);

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  console.log("selectedRowKeys: ", selectedRowKeys.value);
  await batchDeleteLog({ ids: selectedRowKeys.value }, reload);
}
</script>
